package com.demo.first;

import com.demo.po.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class MyBatisFirst {
    
    @Test
    public void findUserById() throws IOException {
        //1、读取配置文件
        String resource="SqlMapConfig.xml";
        //2、根据配置文件创建SqlSessionFactory
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //3、SqlSessionFactory创建SqlSession
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //4、SqlSession执行statement，并返回映射结果
        //第一个参数：statement的id，建议：namespace.statementId（确保唯一）
        //第二个参数：入参的值，它的类型要和映射文件中对应的statement的入参类型一致
        User user = sqlSession.selectOne("test.findUserById", 1);
        System.out.println(user);
        //5、关闭SqlSession
        sqlSession.close();
    }
    
    @Test
    public void findUserByName() throws IOException {
        //1、读取配置文件
        String resource="SqlMapConfig.xml";
        //2、根据配置文件创建SqlSessionFactory
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //3、SqlSessionFactory创建SqlSession
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //4、SqlSession执行statement，并返回映射结果
        //第一个参数：statement的id，建议：namespace.statementId（确保唯一）
        //第二个参数：入参的值，它的类型要和映射文件中对应的statement的入参类型一致
        List<User> list = sqlSession.selectList("test.findUserByName", "小明");
        System.out.println(list);
        //5、关闭SqlSession
        sqlSession.close();
    }
    
    @Test
    public void insertUser() throws IOException {
        //1、读取配置文件
        String resource="SqlMapConfig.xml";
        //2、根据配置文件创建SqlSessionFactory
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //3、SqlSessionFactory创建SqlSession
        SqlSession sqlSession=sqlSessionFactory.openSession();
        User user=new User();
        user.setUsername("李四");
        user.setAddress("china");
        user.setSex("1");
        
        sqlSession.insert("test.insertUser",user);
        System.out.println(user.getId());
        //提交事务
        sqlSession.commit();
        sqlSession.close();
    }
    
    //删除用户
    @Test
    public void deleteUser() throws IOException {
        //1、读取配置文件
        String resource="SqlMapConfig.xml";
        //2、根据配置文件创建SqlSessionFactory
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //3、SqlSessionFactory创建SqlSession
        SqlSession sqlSession=sqlSessionFactory.openSession();
        
        sqlSession.delete("test.deleteUser",27);
        //提交事务
        sqlSession.commit();
        sqlSession.close();
    }
    
    //修改用户
    @Test
    public void updateUser() throws IOException {
        //1、读取配置文件
        String resource="SqlMapConfig.xml";
        //2、根据配置文件创建SqlSessionFactory
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //3、SqlSessionFactory创建SqlSession
        SqlSession sqlSession=sqlSessionFactory.openSession();
        
        User user=new User();
        user.setUsername("李四");
        user.setAddress("江苏南京");
        user.setSex("2");
        
        sqlSession.update("test.updateUser",user);
        //提交事务
        sqlSession.commit();
        sqlSession.close();
    }
}
